package com.kid.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;

/**
 * EnumConstraintValidator是真正的校验器,实现接口ConstraintValidator 
 * 校验逻辑写在这里
 */
public class EnumConstraintValidator implements ConstraintValidator<EnumValidate,Integer> {


    private String regexp;

    //定义set集合，用于收集EnumValidate传入的values
    private Set<Integer> set = new HashSet<>();
    @Override
    public void initialize(EnumValidate constraintAnnotation) {
        //通过注解获取values
        int[] values = constraintAnnotation.values();
        for (int value : values) {
            //System.out.println("value="+value);
            set.add(value);
        }
//        regexp = constraintAnnotation.regexp();
    }


    //返回ture，表示验证成功
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        //System.out.println("value:"+value);
        return set.contains(value);
//        return value.toString().matches(regexp);
    }
}
